package com.baidu.eureka.controller.user;

import com.baidu.eureka.entity.User;
import com.baidu.eureka.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-07-18 00:11:44
 */
@Api(tags = "1.0版本-2020720")//
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "id查询")
    @GetMapping("/{id}")
    public User selectOne(@PathVariable("id") Long id) {
        return this.userService.queryById(id);
    }

    /**
     * 查询分页
     *
     * @param page 页码
     * @param size 大小
     * @return 对象集合
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{size}")
    public ResponseEntity<List<User>> queryPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        IPage<User> userList = userService.queryPage(new Page<User>(page, size));
        List<User> users = userList.getRecords();
        return ResponseEntity.ok().body(users);
    }

    /**
     * 添加用户
     * @param user 用户对象
     * @return
     */
    @ApiOperation(value = "添加/修改")
    @ApiOperationSupport(ignoreParameters = {"id"})
    @PostMapping()
    public ResponseEntity<Object> save(@RequestBody User user){
        boolean flae = userService.saveOrUpdate(user, null);
        if(flae){
            return ResponseEntity.status(HttpStatus.OK).body("添加或修改成功");
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("添加或修改失败");
    }

}